package com.spsoft.vatoutinv.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.model.bo.TokenUser;
import com.spsoft.core.model.leqidmz.param.BaseNsrRequestParams;
import com.spsoft.vatoutinv.entity.vmsfullbswjmanager.VmsFullBswjManager;
import com.spsoft.vatoutinv.model.dto.vmsenterpriseinfo.VmsEnterpriseInfoLogPageDto;
import com.spsoft.vatoutinv.model.vo.vmsenterpriseinfo.VmsEnterpriseInfoLogPageVO;
import com.spsoft.vatoutinv.model.vo.vmsenterpriseinfo.VmsEnterpriseInfoLogVO;
import com.spsoft.vatoutinv.model.vo.vmsenterpriseinfo.VmsEnterpriseInfoVO;
import com.spsoft.vatoutinv.service.vmsenterpriseinfo.VmsEnterpriseInfoLogService;
import com.spsoft.vatoutinv.service.vmsenterpriseinfo.VmsEnterpriseInfoService;
import com.spsoft.vatoutinv.service.vmsfullbswjmanager.VmsFullBswjManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 纳税人基本信息查询-企业基本信息、税务信息、财务信息;前端控制器
 * </p>
 *
 * @author wangy
 * @since 2023-11-22
 */
@Api(tags = {"纳税人基本信息查询"})
@Validated
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/vmsEnterpriseInfo")
public class VmsEnterpriseInfoController extends BaseController {

    public static final String LOG_MSG = "(纳税人基本信息控制层)";

    private final VmsEnterpriseInfoService enterpriseInfoService;

    private final VmsEnterpriseInfoLogService enterpriseInfoLogService;

    private final VmsFullBswjManagerService vmsFullBswjManagerService;

    @ApiOperation("根据纳税人编号获取纳税人基本信息")
    @AnonymousAccess(value = "/getInfo", method = RequestMethod.POST, permissionCode = "VMS_BASEINFO")
    @Log(value = "根据纳税人编号获取纳税人基本信息", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXPAYERS_BASIC_INFO)
    public Result<VmsEnterpriseInfoVO> getInfo() {
        log.info("{}根据部门获取纳税人基本信息", LOG_MSG);
        return enterpriseInfoService.getVmsEnterpriseInfo();
    }

    @ApiOperation("请求接口获取纳税人基本信息")
    @AnonymousAccess(value = "/addInfo", method = RequestMethod.POST, permissionCode = "VMS_ENTERPRISE_INFO_ADD")
    @Log(value = "请求接口获取纳税人基本信息", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXPAYERS_BASIC_INFO)
    public Result<Object> addInfo() {
        TokenUser tokenUser = getUser();
        VmsFullBswjManager fullBswjManager = vmsFullBswjManagerService.getOneByWhjg(tokenUser.getDeptCode());
        BaseNsrRequestParams params = new BaseNsrRequestParams();
        params.setNsrsbh(fullBswjManager.getNsrsbh());
        params.setCreateUser(tokenUser.getUserCode());
        params.setDeptCode(tokenUser.getDeptCode());
        log.info("{}请求接口获取纳税人基本信息，入参：{}", LOG_MSG, JSONObject.toJSONString(params));
        return enterpriseInfoService.getVmsEnterpriseInfoByLq(params, tokenUser);
    }

    @ApiOperation("获取详情记录分页列表")
    @AnonymousAccess(value = "/page", method = RequestMethod.POST, permissionCode = "VMS_BASEINFO")
    @Log(value = "获取详情记录分页列表", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXPAYERS_BASIC_INFO)
    public Result<IPage<VmsEnterpriseInfoLogPageVO>> getPage(@RequestBody VmsEnterpriseInfoLogPageDto dto) {
        log.info("{}纳税人基本信息分页查询:{}", LOG_MSG, JSONObject.toJSONString(dto));
        return enterpriseInfoLogService.getPage(dto);
    }

    @ApiOperation("根据ID获取纳税人基本信息记录详情")
    @AnonymousAccess(value = "/getDetail", method = RequestMethod.POST, permissionCode = "VMS_BASEINFO")
    @Log(value = "根据ID获取纳税人基本信息记录详情", type = LogActionType.SELECT, menuName = LogMenuEnum.TAXPAYERS_BASIC_INFO)
    public Result<VmsEnterpriseInfoLogVO> getDetail(@RequestBody JSONObject jsonObject) {
        if (jsonObject == null || StringUtils.isBlank(jsonObject.getString("id"))) {
            return Result.ERROR(ResponseEnum.ILLEGAL_ARGUMENT_ERROR);
        }
        log.info("{}纳税人基本信息详情ID:{}", LOG_MSG, jsonObject.getString("id"));
        return enterpriseInfoLogService.getDetail(jsonObject.getString("id"));
    }
}

